﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;

namespace MarginTrading.Forms
{
    public partial class ConfigsDlg : Form
    {
        public ConfigsDlg()
        {
            InitializeComponent();
        }

        private void ConfigsDlg_Load(object sender, EventArgs e)
        {
            try
            {
	            DataTable configTable = ConfigServices.GetConfigCollectionDataTable();
	            dataGridView.DataSource = configTable;
	
	            dataGridView.Columns[0].Width = 250;
	            dataGridView.Columns[1].Width = 350;
	            dataGridView.Width = 602;
	            Width = (dataGridView.Left * 2) + dataGridView.Width + 6;
            }
            catch (System.Exception ex)
            {
            	
            }
        }

        private void toolStripButtonAdd_Click(object sender, EventArgs e)
        {
            AddConfig oAddConfigDlg = new AddConfig();
            oAddConfigDlg.ShowDialog();

            try
            {
	            DataTable configTable = ConfigServices.GetConfigCollectionDataTable();
	            dataGridView.DataSource = configTable;
	            dataGridView.Refresh();
            }
            catch (System.Exception ex)
            {
            	
            }
        }

        private void toolStripButtonEdit_Click(object sender, EventArgs e)
        {
            EditConfig editConfigDlg = new EditConfig(dataGridView.SelectedRows[0].Cells[0].Value.ToString(),
                dataGridView.SelectedRows[0].Cells[1].Value.ToString());
            editConfigDlg.ShowDialog();

            try
            {
	            DataTable configTable = ConfigServices.GetConfigCollectionDataTable();
	            dataGridView.DataSource = configTable;
	            dataGridView.Refresh();
            }
            catch (System.Exception ex)
            {
            	
            }
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            string configName = dataGridView.SelectedRows[0].Cells[0].Value.ToString();
            DialogResult result = MessageBox.Show("Do you really want to delete configuration " + configName + "?", "Confirmation", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

            if (result != DialogResult.Yes)
            {
                return;
            }

            try
            {
                ConfigServices.Delete(configName);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Failed to delete configuration\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            dataGridView.Rows.RemoveAt(dataGridView.SelectedRows[0].Index);
            MessageBox.Show("configuration deleted successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
